<?php
/**
 *
 */

class NconfigAdmin extends AdminBase {

	private $api = null;
	private $nohf = array();

	/**
	 * 构造函数
	 */
	public function __construct(){
		parent::__construct();
		$this->api = n_api("nconfig");
		
		if (! in_array(N_ACTION, $this->nohf)) {
			n_mod('skin')->header();
		}
	}

	public function index() {
		$mysql = n_api("mysql")->db001;
		$rs = $mysql->select("select * from nconfig_template_main order by id");
		$this->_html("index", $rs);
	}

	public function edit(){
		$param = $this->param('id:uint:ignore');

		if (isset($param['id'])) {
			$mysql = n_api("mysql")->db001;
			$rs = $mysql->get("nconfig_template_main", $param['id']);
		}
		else {
			$rs = array();
		}

		$this->_html('edit',$rs);
	}

	public function save() {
		$param = array('id:uint:ignore', 'pevent:char', 'descript:char:empty',
			'name:char', 'ckey:char', 'sington:char');
		$param = $this->param(implode(',', $param));
		$mysql = n_api("mysql")->db001;

		// 单例模板检查
		if ($param['sington'] == 'Y') {
			$sql = "SELECT scode, COUNT(id) AS count FROM nconfig_data_site
					WHERE template_id = '{$param['id']}' GROUP BY scode";
			$data = $mysql->select($sql);
			$sites = array();
			$nsite = n_api('nsite');
			foreach ($data as $row) {
				if ($row['count'] > 1) {
					$sinfo = $nsite->getByScode($row['scode']);
					$sites[] = $sinfo['name'];
				}
			}
			if (! empty($sites)) {
				$msg = "该模板不能设置为单例模式，以下站点使用该模板生成了多条规则:";
				$sites = implode('<br/>', $sites);
				throw new PException($msg.'<br/>'.$sites);
			}
			else {
				$param['is_sington'] = 'Y';
			}
		}
		else {
			$param['is_sington'] = 'N';
		}
		unset($param['sington']);

		$mysql->save("nconfig_template_main",$param);
		n_mod("skin")->save("保存成功", 3, n_url('nconfig', 'index'));
	}

	public function del() {
		$param = $this->param('id:char');
		$mysql = n_api("mysql")->db001;
		$rs = $mysql->get("nconfig_template_main",$param['id']);
		if(empty($rs)){
			throw new PException("找不到配置模板");
		}
		$mysql->del('nconfig_template_main',$param['id']);
		n_mod("skin")->save("删除成功", 0, n_url('nconfig', 'index'));
	}

	public function undel(){
		$param = $this->param('id:char');
		$mysql = n_api("mysql")->db001;
		$rs = $mysql->get("nconfig_template_main",$param['id']);
		if(empty($rs)){
			throw new PException("找不到配置模板");
		}
		$mysql->update('nconfig_template_main',array("isdel"=>'N'),$param['id']);
		n_mod("skin")->save("恢复成功", 3, n_url('nconfig', 'index'));
	}

	public function itemIndex() {
		$rs = $param = $this->param("main_id:uint");
		$mysql = n_api("mysql")->db001;
		$sql = "SELECT * FROM nconfig_template_items
				WHERE main_id = {$param['main_id']}";
		$rs['data'] = $mysql->select($sql);
		$this->_html("itemIndex", $rs);
	}

	public function itemCreate() {
		$param = $this->param("main_id:uint");

		$mysql = n_api("mysql")->db001;
		$template = $mysql->get("nconfig_template_main", $param['main_id']);
		if (empty($template)) {
			throw new PException("找不到配置模板");
		}

		$this->_html('itemEdit', $param);
	}

	public function itemEdit() {
		$param = $this->param("id:uint");

		$mysql = n_api("mysql")->db001;
		$rs = $mysql->get("nconfig_template_items", $param['id']);
		if (empty($rs)) {
			throw new PException("找不到数据项");
		}

		$this->_html("itemEdit", $rs);
	}

	public function itemDel() {
		$param = $this->param("id:uint");
		$mysql = n_api("mysql")->db001;
		$info = $mysql->get("nconfig_template_items", $param['id']);
		if (empty($info)) {
			throw new PException("找不到数据项");
		}
		$mysql->rm("nconfig_template_items", $param['id']);
		$url = n_url('nconfig', 'itemIndex', array('main_id' => $info['main_id']));
		n_mod("skin")->save("删除成功", 0, $url);
	}

	public function itemSave() {
		$param = array('id:uint:ignore', 'main_id:uint', 'itype:uint',
			'ukey:char:empty', 'unit:char', 'option:char', 'default:char',
			'name:char:empty');
		$param = $this->param(implode(',', $param));
		$mysql = n_api("mysql")->db001;

		if (in_array($param['itype'], array(10, 11)) && empty($param['option'])) {
			throw new PException("请填写可选项");
		}

		if (isset($param['id'])) {
			$id = $param['id'];
			unset($param['id']);
			$mysql->update("nconfig_template_items", $param, "id={$id}");
		}
		else {
			$mysql->insert("nconfig_template_items", $param);
		}

		$url = n_url('nconfig', 'itemIndex', array('main_id' => $param['main_id']));
		n_mod("skin")->save("保存成功", 3, $url);
	}

	public function __destruct() {
		if (! in_array(N_ACTION, $this->nohf)) {
			n_mod('skin')->footer();
		}
	}
}
?>
